package  com.diamond.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diamond.bo.ClientOrderBO;
import com.diamond.bo.ClientOrderDetailBO;
import com.diamond.bo.OrderBO;
import com.diamond.bo.QueryOrderBO;
import com.diamond.bo.ReturnBO;
import com.diamond.bo.SuppOrderBO;
import com.diamond.bo.UserBO;
import com.diamond.entity.Clientorder;
import com.diamond.entity.Clientorderdetail;
import com.diamond.facade.ClientorderFacade;
import com.diamond.facade.SupporderFacade;
import com.diamond.facade.UserFacade;
import com.diamond.util.JsonMSG;
import com.diamond.util.JsonUtils;
import com.diamond.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * AorderAction ������
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked")
public class ClientorderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7752356850536860770L;
	private static Logger log=LoggerFactory.getLogger(ClientorderAction.class);
	private HttpServletRequest request;
	private ClientorderFacade clientorderFacade;
	private SupporderFacade supporderFacade;
	private ClientOrderBO clientOrderBo;
	private ClientOrderDetailBO detail;
	private String[] details;
	private List<ClientOrderDetailBO> detailColl;
	private QueryOrderBO queryOrderBo;
	private List<ClientOrderBO> dataList;
	private List  rangList;
	private String json;
	private String tips;
	private String tmpOrderId;
	//-------------------------前台方法------------------------------------
	/**
	 * 从session中添加选中钻石
	 */
	public String addDia(){
		clientorderFacade.addDia(ActionContext.getContext().getSession(),ServletActionContext.getRequest());
		return SUCCESS;
	}
	/**
	 * 添加钻托
	 */
	public String addInlay(){
		clientorderFacade.addInlay(ActionContext.getContext().getSession(),ServletActionContext.getRequest(),detail);
		return SUCCESS;
	}
	/**
	 * 从session中删除选中钻石
	 */
	public String delDia(){
		clientorderFacade.delDia(ActionContext.getContext().getSession(),ServletActionContext.getRequest());
		return SUCCESS;
	}
	/**
	 * 从session中删除全部选中钻石
	 */
	public String delAllDia(){
		ActionContext.getContext().getSession().remove(ActionContext.getContext().getSession().get("USER_ID")+"_selected");
		return SUCCESS;
	}
	/**
	 * 转到临时订单
	 */
	public String toTmpOrder(){
		Map session = ActionContext.getContext().getSession(); 
		Object id=session.get("USER_ID");
		if(id==null)return SUCCESS;
		Object obj=session.get(id+"_selected");
		if(obj!=null){
		   dataList=(List)obj;
		}
		return SUCCESS;
	}
	/**
	 * 保存订单
	 */
	public String saveOrder(){
		String total=ServletActionContext.getRequest().getParameter("total");
		if(details==null||details.length==0||clientOrderBo==null||total==null||"".equals(total))return SUCCESS;
		clientOrderBo.setTotal(Double.parseDouble(total));
		try{
			Map session = ActionContext.getContext().getSession();
			Object userId = session.get("USER_ID");
			if(userId==null)return SUCCESS;
			List list = (List) session.get(session.get("USER_ID")+"_selected");
			if(list==null&&list.size()==0)return SUCCESS;
			List selected=new ArrayList();
			for(int j=0;j<details.length;j++){
				for(int i=0;i<list.size();i++){
					ClientOrderDetailBO bo = (ClientOrderDetailBO) list.get(i);
					if(details[j].equals(bo.getBatch())){
						Clientorderdetail dest = new Clientorderdetail();
						PropertyUtils.copyProperties(dest, bo);
						selected.add(dest);
						break;
					}
				}
			}
			clientorderFacade.saveOrderAndDetail(clientOrderBo,selected,userId,session.get("ANGENT_ID"));
			session.remove(session.get("USER_ID")+"_selected");
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 修改订单
	 */
	public String updateOrder(){
		Object id = ActionContext.getContext().getSession().get("USER_ID");
	    if(id==null)return SUCCESS;
	    Map map=clientorderFacade.updateOrder(clientOrderBo);
		if(map!=null){
			Clientorder bo=(Clientorder) map.get("order");
			try {
				if(bo!=null){
					clientOrderBo = new ClientOrderBO();
					PropertyUtils.copyProperties(clientOrderBo, bo);
				}
				dataList=(List) map.get("details");
				rangList=(List) map.get("rangDetails");
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return SUCCESS;
	}
	/**
	 * 修改订单
	 */
	public String delOrder(){
		Object userType = ActionContext.getContext().getSession().get("USER_TYPE");
	    if(userType==null||!"C".equals(userType))return SUCCESS;
		String orderId=ServletActionContext.getRequest().getParameter("orderid");
		if(StringUtils.isEmpty(orderId))return SUCCESS;
		clientorderFacade.delOrder(Long.parseLong(orderId));
		return SUCCESS;
	}
	/**
	 * 查看订单列表
	 */
	public String toOrderList(){
		String url="toorderlist.action?a=1";
		Map session = ActionContext.getContext().getSession(); 
	    Object obj=session.get("USER_ID");
	    if(obj==null) {
	    	return SUCCESS;
	    } 
	    
	    try{
	    	Long id=(Long) obj;
	    	String HQL="from Clientorder where userId="+id+" and delState ='0' order by orderDate desc";
	    	List list = Page.util(ServletActionContext.getRequest(),url,clientorderFacade,HQL,15);
	    	if(list!=null&&list.size()>0){
	    		dataList = new ArrayList<ClientOrderBO>();
	    		for(int i=0;i<list.size();i++){
	    			Clientorder clientorder = (Clientorder) list.get(i);
	    			ClientOrderBO dest = new ClientOrderBO();
	    			PropertyUtils.copyProperties(dest, clientorder);
	    			dataList.add(dest);
	    		}
	    	}
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
		return SUCCESS;
	}
	/**
	 * 前端根据条件搜索订单
	 * @return
	 */
	public String findOrderList() {
		request = ServletActionContext.getRequest();
		Map session = ActionContext.getContext().getSession(); 
		Integer pageSize = 15;
	    Object obj=session.get("USER_ID");
	    if(obj==null) {
	    	return SUCCESS;
	    }
	    try{
	    	Long userId = (Long)obj;
	    	if (StringUtil.isEmpty(clientOrderBo)) {
	    		clientOrderBo = (ClientOrderBO) request.getSession().getAttribute("clientOrderBo");
	    	}

	    	List list = clientorderFacade.findOrderList(userId, clientOrderBo,clientorderFacade,pageSize);
	    	if(list!=null&&list.size()>0){
	    		dataList = new ArrayList<ClientOrderBO>();
	    		for(int i=0;i<list.size();i++){
	    			Clientorder clientorder = (Clientorder) list.get(i);
	    			ClientOrderBO dest = new ClientOrderBO();
	    			PropertyUtils.copyProperties(dest, clientorder);
	    			dataList.add(dest);
	    		}
	    	}
	    	request.getSession().setAttribute("clientOrderBo", clientOrderBo);
	    } catch(Exception e) {
	    	e.printStackTrace();
	    }
	    
		return SUCCESS;
	}
	
	/**
	 * 用户中心查看订单列表
	 */
	public String toCenOrderList(){
		String url="tocenorderlist.action?a=1";
		Map session = ActionContext.getContext().getSession(); 
	    Object obj=session.get("USER_ID");
	    if(obj==null)return SUCCESS;
	    try{
	    	Long id=(Long) obj;
	    	String HQL="from Clientorder where userId="+id+" and delState ='0' order by orderDate desc";
	    	List list = Page.util(ServletActionContext.getRequest(),url,clientorderFacade,HQL,15);
	    	if(list!=null&&list.size()>0){
	    		dataList = new ArrayList<ClientOrderBO>();
	    		for(int i=0;i<list.size();i++){
	    			Clientorder clientorder = (Clientorder) list.get(i);
	    			ClientOrderBO dest = new ClientOrderBO();
	    			PropertyUtils.copyProperties(dest, clientorder);
	    			dataList.add(dest);
	    		}
	    	}
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
		return SUCCESS;
	}
	
	/**
	 * 用户中心根据条件搜索订单
	 * @return
	 */
	public String findCentOrderList() {
		request = ServletActionContext.getRequest();
		Map session = ActionContext.getContext().getSession(); 
		Integer pageSize = 15;
	    Object obj=session.get("USER_ID");
	    if(obj==null) {
	    	return SUCCESS;
	    }
	    try{
	    	Long userId = (Long)obj;
	    	if (StringUtil.isEmpty(clientOrderBo)) {
	    		clientOrderBo = (ClientOrderBO) request.getSession().getAttribute("clientOrderBo");
	    	}

	    	List list = clientorderFacade.findCentOrderList(userId, clientOrderBo,clientorderFacade,pageSize);
	    	if(list!=null&&list.size()>0){
	    		dataList = new ArrayList<ClientOrderBO>();
	    		for(int i=0;i<list.size();i++){
	    			Clientorder clientorder = (Clientorder) list.get(i);
	    			ClientOrderBO dest = new ClientOrderBO();
	    			PropertyUtils.copyProperties(dest, clientorder);
	    			dataList.add(dest);
	    		}
	    	}
	    	request.getSession().setAttribute("clientOrderBo", clientOrderBo);
	    } catch(Exception e) {
	    	e.printStackTrace();
	    }
	    
		return SUCCESS;
	}
	
	/**
	 * 查看订单明细
	 */
	public String toOrderDetail(){
		String id=ServletActionContext.getRequest().getParameter("orderid");
		if(StringUtil.isEmpty(id))return SUCCESS;
		Map map=clientorderFacade.findOrder(new Long(id));
		if(map!=null){
			Clientorder bo=(Clientorder) map.get("order");
			try {
				if(bo!=null){
					clientOrderBo = new ClientOrderBO();
					PropertyUtils.copyProperties(clientOrderBo, bo);
				}
				dataList=(List) map.get("details");
				rangList=(List) map.get("rangDetails");
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return SUCCESS;
	}
	
	
	//-------------------------前台方法------------------------------------
	
/******************************************后台方法***************************************************************/
	/**
	 * 查询客户订单
	 */
	public String queryClientOrder() {
		request = ServletActionContext.getRequest();
		String start = request.getParameter("start");
		String pageSize = request.getParameter("limit");
		queryOrderBo=(QueryOrderBO) JsonUtils.toObject(json, QueryOrderBO.class);
		if (null == start || "".equals(start) || null == pageSize || "".equals(pageSize)) {
			queryOrderBo.setStart(0);
			queryOrderBo.setPageSize(20);
		} else {
			queryOrderBo.setStart(Integer.valueOf(start));
			queryOrderBo.setPageSize(Integer.valueOf(pageSize));
		}
		try {
			if ("1".equals(queryOrderBo.getOrderType())) {
				ReturnBO<SuppOrderBO> returnBo = supporderFacade.querySuppOrder(queryOrderBo);
				this.json=JsonMSG.sucess(returnBo).toJson();
			} else if ("2".equals(queryOrderBo.getOrderType())) {
				ReturnBO<ClientOrderBO> returnBo = clientorderFacade.queryClientOrder(queryOrderBo);
				this.json=JsonMSG.sucess(returnBo).toJson();
			} 
		} catch (Exception e) {
			e.printStackTrace();
			this.json=JsonMSG.failed("查询失败").toJson();
			log.error("查询异常!",e);
		}
		return "json";
	}
	
	/**
	 * 查询客户订单明细
	 */
	public String queryClientOrderById() {
		try {
			String cliOrdMasterId = json;
			OrderBO orderBo = clientorderFacade.queryClientOrderById(cliOrdMasterId);
			this.json = JsonMSG.sucess(orderBo).toJson();
		} catch (Exception e) {
			e.printStackTrace();
			this.json = JsonMSG.failed("查询失败").toJson();
			log.error("查询异常!", e);
		}
		return "json";
	}
	
	/**
	 * 批量软删除客户订单
	 */
	public String deleteCliOrder() {
		try {
			if (StringUtil.isEmpty(json)) {
				this.json = JsonMSG.failed("请选择需要删除的订单！").toJson();
			} else {
				String[] cliOrderMasterIds = json.split(";");
				Boolean result = clientorderFacade.deleteCliOrder(cliOrderMasterIds);
				if (result)
					this.json = JsonMSG.sucess("订单删除成功！").toJson();
				else 
					this.json = JsonMSG.failed("订单删除失败！").toJson();
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.json = JsonMSG.failed("删除订单出错!详细:" + e).toJson();
			log.error("删除订单出错!详细:", e);
		}
		return "json";
	}
 
	public ClientOrderBO getAorder() {
		return clientOrderBo;
	}

	public void setAorder(ClientOrderBO aorderBo) {
		this.clientOrderBo = aorderBo;
	}
    
	public String getTips() {
		return tips;
	}

	public void setTips(String tips) {
		this.tips = tips;
	}

	public List<ClientOrderBO> getDataList() {
		return dataList;
	}

	public void setDataList(List<ClientOrderBO> dataList) {
		this.dataList = dataList;
	}


	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public ClientOrderBO getClientOrderBo() {
		return clientOrderBo;
	}

	public void setClientOrderBo(ClientOrderBO clientOrderBo) {
		this.clientOrderBo = clientOrderBo;
	}

	public void setClientorderFacade(ClientorderFacade clientorderFacade) {
		this.clientorderFacade = clientorderFacade;
	}

	public QueryOrderBO getQueryOrderBo() {
		return queryOrderBo;
	}

	public void setQueryOrderBo(QueryOrderBO queryOrderBo) {
		this.queryOrderBo = queryOrderBo;
	}

	public String getTmpOrderId() {
		return tmpOrderId;
	}
	public void setTmpOrderId(String tmpOrderId) {
		this.tmpOrderId = tmpOrderId;
	}
	
	public String[] getDetails() {
		return details;
	}
	public void setDetails(String[] details) {
		this.details = details;
	}
	public List<ClientOrderDetailBO> getDetailColl() {
		return detailColl;
	}
	public void setDetailColl(List<ClientOrderDetailBO> detailColl) {
		this.detailColl = detailColl;
	}
	public void setSupporderFacade(SupporderFacade supporderFacade) {
		this.supporderFacade = supporderFacade;
	}
	public List getRangList() {
		return rangList;
	}
	public void setRangList(List rangList) {
		this.rangList = rangList;
	}
	public ClientOrderDetailBO getDetail() {
		return detail;
	}
	public void setDetail(ClientOrderDetailBO detail) {
		this.detail = detail;
	}
	
}
